package com.ruigi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruigi.mapper.EmployeeMapper;
import com.ruigi.pojo.Employee;
import com.ruigi.pojo.R;
import com.ruigi.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class employeeserviceImpl extends ServiceImpl<EmployeeMapper, Employee>  implements EmployeeService {

    @Override
    public R<String> save1(HttpServletRequest request, Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        Long empid= (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empid);
//        employee.setUpdateUser(empid);
        save(employee);


        return R.success("新增成功  ");
    }

    @Override
    public R<Page> page1(int page, int pageSize, String name) {
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        LambdaQueryWrapper.ne(Employee::getUsername, "admin"); // 添加 name != "admin" 的条件
        LambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        page(page1,LambdaQueryWrapper);


        return R.success(page1);
    }

    @Override
    public R<String> update(HttpServletRequest request, Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        Long updateuser = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(updateuser);
        updateById(employee);

        return R.success("");
    }

    @Override
    public R<Employee> getid(Long id) {
        Employee employee = getById(id);
        if (employee!=null) {
            return R.success(employee);

        }
        return R.error("cuocuocuo！！");

    }


}
